package com.epoint.pjq.core.mqtt;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;

/**
 * 
 * @author lilin
 * @date 2016年9月13日 下午7:19:30
 * @annotation 推送消息 逻辑处理
 */
public class SMP_MQTT_Bizlogic {

	// 发送日志广播
	public static void sendLogBroadcast(String message) {
		String logMessage = "[" + convertDate(new Date(), "HH:mm:ss") + "]"+ message;
		Intent intent = new Intent(SMP_MQTT_Service.ACTION_INTENT_RECEIVER_LOG);
		intent.putExtra("logMessage", logMessage);

		SMP_MQTT_Service.getInstance().sendBroadcast(intent);
	}

	// 发送MQTT消息广播
	public static void sendMQTTMSGBroadcast(String message) {
		Intent intent = new Intent(SMP_MQTT_Service.ACTION_INTENT_RECEIVER_USERINFO);
		intent.putExtra("UserInfoMessage", message);

		SMP_MQTT_Service.getInstance().sendBroadcast(intent);
	}

	// 获取MQTT连接状态
	public static void getConnectionStatus() {
		if (SMP_MQTT_EpointClient.getInstance().isConnected()) {
			sendLogBroadcast("状态：已经连接..");
		} else {
			sendLogBroadcast("状态：没有连接..");
		}
	}

	// 处理收到的消息
	public static void dealMessage(String message) {
		SMP_MQTT_Bizlogic.sendLogBroadcast("<font color=red>消息到达：" + message+ "</font>");
		String backstring = new String(message);
		SMP_MQTT_Bizlogic.sendMQTTMSGBroadcast(backstring);
	}

	// 日期转换工具
	public static String convertDate(Date date, String format) {
		if (date != null) {
			DateFormat format1 = new SimpleDateFormat(format);
			String s = format1.format(date);
			return s;
		}
		return "";
	}
}
